package com.tongzhan.controller;

import com.tongzhan.base.BaseController;
import com.tongzhan.base.Return;
import com.tongzhan.entity.User;
import com.tongzhan.entity.party.Discuss;
import com.tongzhan.entity.party.Participation;
import com.tongzhan.entity.party.Reply;
import com.tongzhan.utils.DateSwitcher;
import com.tongzhan.utils.SnowFlakeUtils;
import com.tongzhan.vo.ParticipationVo;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 参政议政
 *
 * @author htmic
 * @date 2020/7/29
 */
@RequestMapping("/socail")
@RestController
public class SocailController extends BaseController {

    /**
     * 添加
     * @param action_picture
     * @param title
     * @param participation_content
     * @param time
     * @param userId
     * @return
     * @throws ParseException
     */
    @PostMapping("/addParticipation")
    public Return addUser(String action_picture, String title, String participation_content, String time, String userId) throws ParseException {
        if (title == null) {
            return Return.getErrorResult("缺少参数");
        }
        if (title.trim().length() > 10) {
            return Return.getErrorResult("标题过长");
        }
        if (participation_content.trim().length() > 200) {
            return Return.getErrorResult("内容过长");
        }
        SnowFlakeUtils snowFlake = new SnowFlakeUtils(7, 7);
        Participation participation = new Participation();
        participation.setPid(String.valueOf(snowFlake.nextId()));
        participation.setAction_picture(action_picture);
        participation.setTitle(title);
        participation.setParticipation_content(participation_content);
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (time != null) {
            Date timeDate = sDateFormat.parse(time);
            if(DateSwitcher.compareDate(timeDate,new Date())>0){
                return Return.getErrorResult("发布时间不能超过当前时间");
            }
            participation.setTime(timeDate);
        }
        User userById = socailService.findUserById(userId);
        if (userById != null) {
            participation.setAdd_userid(userId);
            participation.setAdd_username(userById.getUserName());
        }
        long l = socailService.addParticipation(participation);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }

        return Return.getErrorResult("新增失败!");
    }

    /**
     * 详情
     * @param pid
     * @return
     */
    @PostMapping("/participationDetail")
    public Return participationDetail(String pid,String userId) {
        if (pid == null) {
            return Return.getErrorResult("缺少id参数!");
        }
        ParticipationVo participationVo = socailService.participationDetail(pid,userId);
        return Return.getSuccessResult(participationVo);
    }

    /**
     * 列表
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/participationList")
    public Return participationList(Integer page, Integer size) {
        page = page == null ? 0 : page - 1;
        Page<Participation> list = socailService.participationList(page, size);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list.getContent());
        map.put("total", list.getTotalElements());
        map.put("totalpage", list.getTotalPages());
        return Return.getSuccessResult(map);
    }

    /**
     * 添加评论
     * @param userId
     * @param discuss_comeid
     * @param discuss_content
     * @return
     * @throws ParseException
     */
    @PostMapping("/adddiscuss")
    public Return adddiscuss(String userId, String discuss_comeid, String discuss_content,String discuss_pic) throws ParseException {
        if (userId == null) {
            return Return.getErrorResult("缺少参数");
        }
        if (discuss_content.length() > 300) {
            return Return.getErrorResult("评论内容过长");
        }
        SnowFlakeUtils snowFlake = new SnowFlakeUtils(7, 7);
        Discuss discuss = new Discuss();
        discuss.setDiscuss_content(discuss_content);
        discuss.setDiscuss_comeid(discuss_comeid);
        discuss.setDid(String.valueOf(snowFlake.nextId()));
        discuss.setDiscuss_pic(discuss_pic);
        User userById = socailService.findUserById(userId);
        if (userById != null) {
            discuss.setDiscuss_person_id(userById.getUserId());
            discuss.setUsername(userById.getUserName());
        }
        long l = socailService.adddiscuss(discuss);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }
        return Return.getErrorResult("新增失败!");
    }

    /**
     * 添加回复
     * @param userId
     * @param did
     * @param reply_content
     * @return
     * @throws ParseException
     */
    @PostMapping("/addreply")
    public Return addreply(String userId, String did, String reply_content) throws ParseException {
        if (userId == null) {
            return Return.getErrorResult("缺少参数");
        }
        if (reply_content.length() > 300) {
            return Return.getErrorResult("回复内容过长");
        }
        SnowFlakeUtils snowFlake = new SnowFlakeUtils(7, 7);
        Reply reply = new Reply();
        reply.setRid(String.valueOf(snowFlake.nextId()));
        reply.setDid(did);
        reply.setReply_content(reply_content);
        User userById = socailService.findUserById(userId);
        if (userById != null) {
            reply.setReply_person_id(userById.getUserId());
            reply.setReplyName(userById.getUserName());
        }
        long l = socailService.addreply(reply);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }
        return Return.getErrorResult("新增失败!");
    }

    /**
     * 删除回复
     * @param rid
     * @return
     * @throws ParseException
     */
    @PostMapping("/delreply")
    public Return delreply(String rid) throws ParseException {
        if (rid == null) {
            return Return.getErrorResult("缺少参数");
        }
        long l = socailService.delreply(rid);
        if (l > 0) {
            return Return.getSuccessResult("删除成功!");
        }
        return Return.getErrorResult("删除失败!");
    }

    /**
     * 删除评论
     * @param did
     * @return
     * @throws ParseException
     */
    @PostMapping("/deldiscuss")
    public Return deldiscuss(String did) throws ParseException {
        if (did == null) {
            return Return.getErrorResult("缺少参数");
        }
        long l = socailService.deldiscuss(did);
        if (l > 0) {
            return Return.getSuccessResult("删除成功!");
        }
        return Return.getErrorResult("删除失败!");
    }
}
